import { ServerRouteHttpClient, updateHttpSetting } from "@/axios/HttpClient";
import { store, storeCommitPath } from "@/store/index";

import APIConst from "@/common/constants/APIConst";
import { APIResponse } from "@/common/model/APIResponse";
import SweetAlertService from "./SweetAlertService";
import LangConst from "../constants/LangConst";

const HttpService = {
  async setupServerRouter(compCode: string) {
    const reqObj = {
      compCode: compCode,
    };
    const ServerRouterResponse = await ServerRouteHttpClient.post<APIResponse>(
      APIConst.authentication.getserver,
      reqObj
    );

    if (!ServerRouterResponse.data.IsSuccess) {
      SweetAlertService.warning(
        store.getters.t(LangConst.Warn.warn),
        store.getters.t(LangConst.Warn.warn)
      );
      return Promise.resolve(false);
    }

    // const currentUrl = "http://localhost:59734/";
    const currentUrl = ServerRouterResponse.data.Data;
    const url =
      process.env.NODE_ENV == "development"
        ? currentUrl
        : ServerRouterResponse.data.Data;

    store.commit(storeCommitPath.setCompCode, compCode);
    store.commit(storeCommitPath.setUrl, url);
    updateHttpSetting(url);

    return Promise.resolve(true);
  },
};
export default HttpService;
